#include <cmath>

#include "ConstraintY.h"

namespace BlobSalladDS
{
	ConstraintY::ConstraintY(PointMass *pointMass, double y, double shortConst, double longConst)
	{
		this->pointMass = pointMass;
		this->y = y;
		this->delta = new Vector(0.0, 0.0);
		this->shortConst = shortConst;
		this->longConst = longConst;
	}

	void ConstraintY::sc()
	{
		double dist;

		dist = fabs(this->pointMass->getYPos() - this->y);
		this->delta->setY(-dist);

		if (this->shortConst != 0.0 && dist < this->shortConst)
		{
			double scaleFactor;

			scaleFactor = this->shortConst / dist;
			this->delta->scale(scaleFactor);
			pointMass->getPos()->sub(*(this->delta));
		}
		else if (this->longConst != 0.0 && dist > this->longConst)
		{
			double scaleFactor;

			scaleFactor = this->longConst / dist;
			this->delta->scale(scaleFactor);
			pointMass->getPos()->sub(*(this->delta));
		}
	}
}
